﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Vessel_calculation
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		private Vessel_calculation.Classes.Ship Vessel = new Vessel_calculation.Classes.Ship();
		private string file;
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
		    
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		
		void ButtonReadClick(object sender, EventArgs e)
		{
            dataGridView1.ColumnCount = 13;
            dataGridView1.RowCount = Vessel.waterlines + 1;
			
            string[] first = new string[] {"№ of bends","S","My","Ix","Iy","xf","Iyf",
            							   "V", "xc", "zc", "R", "r", "zm"};
            
            for (int i=0; i < dataGridView1.ColumnCount; i++)
            {
            	dataGridView1[i,0].Value = first[i];
            }
            
            for (int i=0; i < Vessel.waterlines; i++)
            {
            	dataGridView1[0,i+1].Value = Convert.ToString(i);
            	dataGridView1[1,i+1].Value = Convert.ToString(Vessel.S[i]);
            	dataGridView1[2,i+1].Value = Convert.ToString(Vessel.My[i]);
            	dataGridView1[3,i+1].Value = Convert.ToString(Vessel.Ix[i]);
            	dataGridView1[4,i+1].Value = Convert.ToString(Vessel.Iy[i]);
            	dataGridView1[5,i+1].Value = Convert.ToString(Vessel.xf[i]);
            	dataGridView1[6,i+1].Value = Convert.ToString(Vessel.Iyf[i]);
            	dataGridView1[7,i+1].Value = Convert.ToString(Vessel.V[i]);
            	dataGridView1[8,i+1].Value = Convert.ToString(Vessel.xc[i]);
            	dataGridView1[9,i+1].Value = Convert.ToString(Vessel.zc[i]);
            	dataGridView1[10,i+1].Value = Convert.ToString(Vessel.R[i]);
            	dataGridView1[11,i+1].Value = Convert.ToString(Vessel.r[i]);
            	dataGridView1[12,i+1].Value = Convert.ToString(Vessel.zm[i]);
            	
            }
		}
		
		void ButtonCalcClick(object sender, EventArgs e)
		{
			Classes.Ship_Parameters parameters;
			parameters = new Classes.Ship_Parameters(Convert.ToDouble(textBoxLpp.Text),
			                             		     Convert.ToDouble(textBoxLB.Text),
			                               			 Convert.ToDouble(textBoxBT.Text));
			textBoxB.Text = Convert.ToString(parameters.B);
			textBoxT.Text = Convert.ToString(parameters.T);
			
			Classes.ReadFile draw = new Classes.ReadFile(file);
			
			Classes.Bow_coeffitients.Calc_bow_coeffitients(parameters.Lpp,
			                                               draw.dL,
			                                               draw.waterlines,
			                                               ref draw.xf,
			                                               ref draw.xa,
			                                               out Vessel.kb,
			                                               out Vessel.ks);
			
			Vessel.parameters = parameters;
			Vessel.drawing = draw.drawing;
			Vessel.bends = draw.bends;
			Vessel.waterlines = draw.waterlines;
			Vessel.HydrostaticCalc();
		}
		
		void OpenToolStripMenuItemClick(object sender, System.EventArgs e)
		{
			OpenFileDialog fileName = new OpenFileDialog();
			
			fileName.InitialDirectory = "C:\\";
			fileName.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*" ;
   		    fileName.FilterIndex = 1 ;
            fileName.RestoreDirectory = true ;
			
			fileName.ShowDialog();
			
			file = fileName.FileName;
		}
	}
}
